package com.situ.crm.dao.impl;

import com.situ.crm.dao.IDeptDao;
import com.situ.crm.pojo.Dept;
import com.situ.crm.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

//Alt+Enter
public class DeptDaoImpl implements IDeptDao {
    @Override
    public List<Dept> selectAll(String searchName) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        List<Dept> list = new ArrayList<>();
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,address,phone,create_time,update_time from dept where name like ?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, "%" + searchName + "%");
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                int id = resultSet.getInt("id");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                String phone = resultSet.getString("phone");
                //没遍历一次就查询出dept表中的一条数据，封装成一个Dept对象
                Dept dept = new Dept();
                dept.setId(id);
                dept.setName(name);
                dept.setAddress(address);
                dept.setPhone(phone);
                list.add(dept);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public void deleteById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "delete from dept where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void add(Dept dept) {
        //链接
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "insert into dept(name,address,phone) values(?,?,?)";
            statement = connection.prepareStatement(sql);
            statement.setString(1, dept.getName());
            statement.setString(2, dept.getAddress());
            statement.setString(3, dept.getPhone());
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Dept selectById(int id) {
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        Dept dept = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "select id,name,address,phone,create_time,update_time from dept where id=?";
            statement = connection.prepareStatement(sql);
            statement.setInt(1, id);
            System.out.println(statement);
            resultSet = statement.executeQuery();
            while (resultSet.next()) {
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                String phone = resultSet.getString("phone");
                //没遍历一次就查询出dept表中的一条数据，封装成一个Dept对象
                dept = new Dept();
                dept.setId(id);
                dept.setName(name);
                dept.setAddress(address);
                dept.setPhone(phone);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return dept;
    }

    @Override
    public void update(Dept dept) {
        Connection connection = null;
        PreparedStatement statement = null;
        try {
            connection = JDBCUtil.getConnection();
            String sql = "update dept set name=?,address=?,phone=? where id=?";
            statement = connection.prepareStatement(sql);
            statement.setString(1, dept.getName());
            statement.setString(2, dept.getAddress());
            statement.setString(3, dept.getPhone());
            statement.setInt(4, dept.getId());
            statement.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
